package com.sc.nft.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.sc.nft.dao.*;

import com.sc.nft.entity.BoxEquity;
import com.sc.nft.entity.BoxItem;
import com.sc.nft.entity.EquityProps;
import com.sc.nft.entity.SysMember;
import com.sc.nft.entity.dto.AddBoxEquityItemDTO;
import com.sc.nft.entity.vo.BoxCollectionEquityVO;
import com.sc.nft.enums.MallExchangeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.service.BoxEquityService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 服务接口实现
 *
 * @author Tangzehui
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-11-09
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BoxEquityServiceImpl implements BoxEquityService {
    private final BoxEquityDao boxEquityDao;
    private final BoxItemDao boxItemDao;
    private final EquityPropsDao equityPropsDao;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addBoxEquityList(Long boxId, Long boxItemId, List<BoxEquity> boxEquities) {
        //校验藏品是否属于当前盲盒
        BoxItem boxItem = boxItemDao.getById(boxItemId);
        if (!boxItem.getBoxId().equals(boxId)) {
            throw new GlobalRunTimeException("当前盲盒藏品不属于当前盲盒！");
        }

        //存在旧配置时先删除旧配置
        if (CollectionUtil.isNotEmpty(boxEquityDao.getByBoxItemId(boxItemId))) {
            boxEquityDao.deleteByBoxItemId(boxItemId);
        }

        if (CollectionUtil.isNotEmpty(boxEquities)) {
            boxEquities.forEach(b -> {
                if (ObjectUtil.isNull(b.getLinkId())){
                    b.setLinkName(MallExchangeEnum.INTEGRAL.getDesc());
                }
                //权益道具
                else {
                    EquityProps equityProps = equityPropsDao.getById(b.getLinkId());
                    b.setLinkName(equityProps.getFirstTitle());
                }
                b.setBoxId(boxId);
                b.setCollectionId(boxItem.getCollectionId());
                b.setBoxItemId(boxItemId);
            });
            //放入新的配置
            boxEquityDao.saveList(boxEquities);
        }

    }

    @Override
    public List<BoxCollectionEquityVO> getBoxEquity(Long boxId) {
        List<BoxCollectionEquityVO> result = boxItemDao.getBoxItemByBoxId(boxId);
        if (CollectionUtil.isEmpty(result)) {
            return Lists.newArrayList();
        }

        List<Long> boxItemIdList = result.stream().map(BoxCollectionEquityVO::getBoxItemId).collect(Collectors.toList());
        List<BoxEquity> equityItemList = boxEquityDao.selectByBoxItemId(boxItemIdList);
        if (CollectionUtil.isEmpty(equityItemList)) {
            return result;
        }
        Map<Long, List<BoxEquity>> equityMapByBoxItemId = equityItemList.stream().collect(Collectors.groupingBy(BoxEquity::getBoxItemId));
        result.forEach(r -> {
            List<BoxEquity> boxEquities = equityMapByBoxItemId.get(r.getBoxItemId());
            if (CollectionUtil.isNotEmpty(boxEquities)) {
                r.setBoxEquityItems(boxEquities.stream().map(boxEquity -> BeanUtil.copyProperties(boxEquity, AddBoxEquityItemDTO.class)).collect(Collectors.toList()));
            }
        });
        return result;
    }
}